package cn.com.dycc.topic_activity.service;


import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.common.log.Constants;
import cn.com.dycc.common.log.SystemServiceLog;
import cn.com.dycc.party_org.organize.dao.OrganizeDao;

import cn.com.dycc.party_org.organize.param.PartyInfoParam;
import cn.com.dycc.party_org.organize.service.OrganizeService;
import cn.com.dycc.sys_manage.deptsysmanagement.dao.DeptDao;
import cn.com.dycc.topic_activity.dao.Pb_Activity_EnterDao;
import cn.com.dycc.topic_activity.dao.Pb_Special_ActivityDao;
import cn.com.dycc.topic_activity.entity.Pb_Activity_Enter;
import cn.com.dycc.topic_activity.entity.Pb_Special_Activity;
import cn.com.dycc.topic_activity.param.Pb_Activity_EnterParam;
import cn.com.dycc.topic_activity.param.Pb_Special_ActivityParam;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.JsonObjectResult;
import cn.com.dycc.util.PageObjectUtil;
import cn.com.dycc.util.StringUtil;




@Service("pb_Activity_EnterService")
@Transactional
public class Pb_Activity_EnterServiceImpl implements Pb_Activity_EnterService{

	@Resource
	private Pb_Activity_EnterDao pb_Activity_EnterDao;
	
	@Resource
	private Pb_Special_ActivityDao pb_Special_ActivityDao;
	
	@Resource
	private DeptDao deptDao;
	
	@Resource
	private OrganizeDao organizeDao;
	
	@Resource
	private OrganizeService organizeService;
	
	
	@SystemServiceLog(description=Constants.ACTIVITY_ENTER_ADD)
	public int addPd_Activity_Enter(Pb_Activity_EnterParam param, HttpServletRequest request)
			throws Pb_Activity_EnterNotFoundException {
		checkParam(param);
		HttpSession session=request.getSession();
		String user_id=(String)session.getAttribute("id");		
		param.setUser_id(user_id);		
		StringBuilder builder=new StringBuilder();
		
		if(param.getActivity_id()==null||param.getActivity_id().trim().isEmpty()){
			builder.append("请选择要报名的专题活动;");
		}
		if(param.getUser_id()==null||param.getUser_id().trim().isEmpty()){
			builder.append("请选择要报名的人员;");
		}
		if(!"".equals(builder.toString())){
			throw new Pb_Activity_EnterNotFoundException(builder.toString());
		}
		Pb_Special_ActivityParam activity_param=new Pb_Special_ActivityParam();
		activity_param.setActivity_id(param.getActivity_id());
		activity_param.setIssue_flag("1");
		activity_param.setStart(0);
		activity_param.setEnd(1);
		List<Pb_Special_Activity> activities=pb_Special_ActivityDao.findPb_Special_ActivityByParam(activity_param);
		if(activities.size()<0){
			throw new Pb_Activity_EnterNotFoundException("要报名的专题活动不存在或未发表");
		}
		Pb_Special_Activity activity=activities.get(0);
		Date enter_date_b=activity.getEnter_date_b();
		Date enter_date_e=activity.getEnter_date_e();
		long now=System.currentTimeMillis();
		long begin=enter_date_b.getTime();
		long end=enter_date_e.getTime()+24*60*60*1000;
		if(now-begin<0){
			throw new Pb_Activity_EnterNotFoundException("专题活动报名未开始,不能报名");
		}
		if(now-end>0){
			throw new Pb_Activity_EnterNotFoundException("专题活动报名已结束,不能报名");
		}
		String count=pb_Activity_EnterDao.getPb_Activity_EnterCountByParam(param);
		if(!"0".equals(count)){
			throw new Pb_Activity_EnterNotFoundException("该人员已报名,不能重复报名");
		}
		
		String org_id=(String)session.getAttribute("org_id");
		String dept_id=(String)session.getAttribute("dept_id");
		param.setDept_id(dept_id);
		if(param.getDept_id()==null||param.getDept_id().trim().isEmpty()){
			builder.append("该用户不是党员,不能报名;");
		}
		param.setState("Y");
		param.setOrg_id(org_id);
		List<Pb_Activity_Enter> lists = getServiceList(param);
		Pb_Activity_Enter entity=lists.get(0);
		int d = pb_Activity_EnterDao.savePb_Activity_Enter(entity);
			
		return d;
		
	}

	

	

	
	
	
	
	/**
	 * 校验 @param param @return @throws
	 */
	public void checkParam(Pb_Activity_EnterParam param) {
		if (param == null) {
			throw new Pb_Activity_EnterNotFoundException("未登录或超时,请重新登录");
		}
		
	}

	public List<Pb_Activity_Enter> getServiceList(Pb_Activity_EnterParam param) {
		Pb_Activity_Enter entity = new Pb_Activity_Enter();

		entity.setActivity_id(param.getActivity_id());
		entity.setUser_id(param.getUser_id());
		entity.setDept_id(param.getDept_id());
		entity.setEnter_date(DateUtil.getNowTimestamp());
		entity.setState(param.getState());
		entity.setOrg_id(param.getOrg_id());
			
		List<Pb_Activity_Enter> lists = new ArrayList<Pb_Activity_Enter>();
		lists.add(entity);
		return lists;
	}
	
	@SystemServiceLog(description=Constants.ACTIVITY_ENTER_DELETE)
	public int deletePb_Activity_Enter(Pb_Activity_EnterParam param, HttpServletRequest request)
			throws Pb_Activity_EnterNotFoundException {
		if(param==null){
			throw new Pb_Activity_EnterNotFoundException("未登录或者登录超时,请重新登录");
		}
		
		if(param.getActivity_id()==null||param.getActivity_id().trim().isEmpty()||param.getUser_id()==null||param.getUser_id().trim().isEmpty()){
			throw new Pb_Activity_EnterNotFoundException("请选择要删除的报名信息");
		}
		int d = pb_Activity_EnterDao.deletePb_Activity_Enter(param);
		return d;
	}




	public JsonObjectResult findPb_Activity_EnterByParam(Pb_Activity_EnterParam param, HttpServletRequest request)
			throws Pb_Activity_EnterNotFoundException {
		List<Pb_Activity_Enter> lists = new ArrayList<Pb_Activity_Enter>();
		
		checkParam(param);
		//20180802 start
		HttpSession session=request.getSession();
		String dept_id=(String)session.getAttribute("dept_id");
		if(dept_id==null){
			throw new Pb_Special_ActivityNotFoundException("该用户不是党员");
		}
		if(param.getUser_id()==null&&"1".equals(param.getLeader_flag())){
			PartyInfoParam partyInfoParam=new PartyInfoParam(); 
			String party_dept=dept_id;
			List<String> dept_party_list=organizeService.findTreeByPidLine(request, partyInfoParam, party_dept);
			if(!dept_party_list.isEmpty()){
				param.setDept_party_list(dept_party_list);
			}else {
				throw new Pb_Special_ActivityNotFoundException("该部门及其子部门下无相应的党支部信息");
			}
			System.out.println("dept_party_list:"+dept_party_list);
		}
		//20180802 end
		param = realParam(param, request);
		String count = pb_Activity_EnterDao.getPb_Activity_EnterCountByParam(param);
		
		if ("0".equals(count)) {			
			return new JsonObjectResult(lists);
		}
		param = (Pb_Activity_EnterParam) PageObjectUtil.getSqlPageObject(count, param);
		lists = pb_Activity_EnterDao.findPb_Activity_EnterByParam(param);
		
		
		return new JsonObjectResult(count,lists);
	}
	
	/**
	 * 实际参数
	 * 
	 * @param param
	 * @param request
	 * @return
	 */
	public Pb_Activity_EnterParam realParam(Pb_Activity_EnterParam param, HttpServletRequest request) {
		if(param.getActivity_name()!=null&&!param.getActivity_name().trim().isEmpty()){
			param.setActivity_name(StringUtil.format_mh_str(param.getActivity_name()));
		}
		if(param.getUser_name()!=null&&!param.getUser_name().trim().isEmpty()){
			param.setUser_name(StringUtil.format_mh_str(param.getUser_name()));
		}
		if(param.getOrder()==null||param.getOrder().isEmpty()||"1".equals(param.getOrder())){
			param.setOrder("p.enter_date desc");
		}else if("2".equals(param.getOrder())){
			param.setOrder("p.enter_date asc");
			
		}else{
			param.setOrder(null);//手动防止SQL注入
		}
		return param;
	}
	
	
}
